#include "populaceitem.h"
#include "toolkit.h"
#include <QPainter>
#include "resources.h"

PopulaceItem::PopulaceItem(MANTYPE manTp):
    ManItem(manTp),
    m_bRuning(false),
    m_bHalf(true),
    m_bDrawSos(false),
    m_SosOpacity(1.0)
{
    m_FloatSpeed = 0.5;
    m_SosImage.load(":/image/man/help_waixingren.png");

    switch(m_manTp)
    {
    case BAOZI_MTP:
    {
        m_animamap[GO_TP] = 3;
        m_animamap[CATCH_TP] = 6;
        m_animamap[TURN_TP] = 9;
        m_animamap[DROP_TP] = 12;
        m_animamap[DROPING_TP] = 14;
        m_animamap[UP_TP] = 17;
        m_animamap[RUN_SQUAT_TP] = 20;
        m_animamap[SOS_FIRE_TP] = 23;

        QTimer::singleShot(20000,this,SLOT(Sos()));

    }
        break;
    case GIRL_MTP:
    {
        m_animamap[GO_TP] = 3;
        m_animamap[CATCH_TP] = 6;
        m_animamap[TURN_TP] = 9;
        m_animamap[DROP_TP] = 11;
        m_animamap[DROPING_TP] = 13;
        m_animamap[UP_TP] = 16;
        m_animamap[RUN_SQUAT_TP] = 18;
        m_animamap[SOS_FIRE_TP] = 23;

        QTimer::singleShot(10000,this,SLOT(Sos()));
    }
        break;
    case CITOU_MTP:
    {
        m_animamap[GO_TP] = 3;
        m_animamap[CATCH_TP] = 6;
        m_animamap[TURN_TP] = 9;
        m_animamap[DROP_TP] = 11;
        m_animamap[DROPING_TP] = 13;
        m_animamap[UP_TP] = 16;
        m_animamap[RUN_SQUAT_TP] = 19;
        m_animamap[SOS_FIRE_TP] = 22;

        QTimer::singleShot(10000,this,SLOT(Sos()));
    }
    default:
        break;
    }

    SetDetectX();
}

void PopulaceItem::logic()
{
    if(m_bRun)
    {
        switch(m_mananimatp)
        {
        case TURN_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                SetAnimaType(m_bRuning ? RUN_SQUAT_TP : GO_TP);
            }
            else
            {
                m_x += m_ManSize.width();
            }
        }
            break;
        case GO_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_x = 0;
            }
            else
            {
                m_x += m_ManSize.width();
            }

            Go();
        }
            break;
        case CATCH_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_x = (m_animamap[m_mananimatp - 1] + 1) * m_ManSize.width();
            }
            else
            {
                m_x += m_ManSize.width();
            }

            Cover();
        }
            break;
        case SOS_FIRE_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_bRuning = true;
                if(IsTurn())
                {
                    Turn();
                }
                else
                {
                    SetAnimaType(RUN_SQUAT_TP);
                }
            }
            else
            {
                m_x += m_ManSize.width();
            }
        }
            break;
        case RUN_SQUAT_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_x = (m_animamap[m_mananimatp - 1] + 1) * m_ManSize.width();
            }
            else
            {
                m_x += m_ManSize.width();
            }

            Run();
        }
            break;
        case DROPING_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_x = (m_animamap[m_mananimatp - 1] + 1) * m_ManSize.width();
            }
            else
            {
                m_x += m_ManSize.width();
            }

            Droping();
        }
            break;
        case UP_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                if(ToolKit::m_bVoice && ToolKit::m_bgameVoice)
                    ToolKit::playSound(12);

                SetAnimaType( SOS_FIRE_TP);
                m_bDrawSos = true;
            }
            else
            {
                m_x += m_ManSize.width();
            }
        }
            break;
        case DROP_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                SetAnimaType(DROPING_TP);
            }
            else
            {
                m_x += m_ManSize.width();
            }
        }
            break;
        default:
            break;
        }

        if(m_bdrawlink)
            DrawLink();

        if(m_bDrawSos)
            SetSosOpac();

        update();
    }
}

void PopulaceItem::SetDetectX()
{
    m_detectX = m_bleft ? static_cast<qreal>(qrand() % 320) :
                          static_cast<qreal>(qrand() % 320) + 320;
}

void PopulaceItem::Go()
{
    if(m_bleft)
    {
        if(x() <= m_detectX)
        {
            m_detectX = static_cast<qreal>(qrand() % 320) + 320;
            Turn();
        }
        else
        {
            setX(x() - 4);
        }
    }
    else
    {
        if(x() >= m_detectX)
        {
            m_detectX = static_cast<qreal>(qrand() % 320);
            Turn();
        }
        else
        {
            setX(x() + 4);
        }
    }
}

bool PopulaceItem::IsTurn()
{
    bool bTurn;
    if(x() <= 320.0)
    {
        bTurn = !m_bleft;
    }
    else
    {
        bTurn = m_bleft;
    }

    return bTurn;
}

void PopulaceItem::Run()
{
    if(x() <= 320.0)
    {
        if(x() <= -50.0)
        {
            SetAnimaType(NULL_TP);
            m_linelist.clear();
            m_PathList.clear();
            setVisible(false);
            emit RunOut(m_manTp,this);
        }
        else
        {
            setX(x() - 8.0);
        }
    }
    else
    {
        if(x() >= 690.0)
        {
            SetAnimaType(NULL_TP);
            m_linelist.clear();
            m_PathList.clear();
            setVisible(false);
            emit RunOut(m_manTp,this);
        }
        else
        {
            setX(x() + 8.0);
        }
    }
}

void PopulaceItem::Sos()
{
    if(m_mananimatp == GO_TP)
    {
        if(ToolKit::m_bVoice && ToolKit::m_bgameVoice)
            ToolKit::playSound(12);

        SetAnimaType(SOS_FIRE_TP);
        m_bDrawSos = true;
    }
}

void PopulaceItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->drawPixmap(QRectF(m_ManPt,m_ManSize),m_animaimage,QRectF(QPointF(m_x,0),m_ManSize));

    if(m_mananimatp == CATCH_TP)
    {
       switch(m_CoverType)
       {
       case COMMON_CTP:
       {
           painter->drawPixmap(QRectF(m_CommonPt,m_CoverCommonSize),m_CoverCommonImage,
                               QRectF(QPointF(m_coverX,0),m_CoverCommonSize));
       }
           break;
       case DIS_CTP:
       {
           painter->drawPixmap(QRectF(m_DisappearPt,m_CoverDisappearSize),m_CoverDisappearImage,
                               QRectF(QPointF(m_coverX,0),m_CoverDisappearSize));
       }
           break;
       case BOMB_CTP:
       {
           painter->drawPixmap(QRectF(m_DisappearPt,m_CoverBombSize),m_CoverBombImage,
                               QRectF(QPointF(m_coverX,0),m_CoverBombSize));
       }
           break;
       default:
           break;
       }
    }

    if(m_bdrawlink)
    {
        painter->save();
        painter->setOpacity(m_linkOpacity);
        painter->drawPixmap(QRectF(20.0,35.0,32.0,36.0),ToolKit::m_rc->m_LinkImage,QRectF(0.0,0.0,32.0,36.0));
        painter->restore();
    }

    if(m_bDrawSos)
    {
        painter->save();
        painter->setOpacity(m_SosOpacity);
        painter->drawPixmap(3,0,m_SosImage);
        painter->restore();
    }

//    painter->drawRect(boundingRect());
}

void PopulaceItem::SetSosOpac()
{
    if(m_SosOpacity >= 0.0)
    {
        m_SosOpacity -= 0.167;
    }
    else
    {
        m_bDrawSos = false;
        m_SosOpacity = 1.0;
    }
}
